package javaBase2;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Random;

public class Game {

    private Card [] initCards = new Card[54];
    private Player [] players = new Player[3];
    public Game(Player[] players) {
        this.players = players;
        // 循环花色
        int index = 0;
        for (CardStyle style : CardStyle.values()){
            for (int i = 2; i <= 14; i ++){
                initCards[index] = new Card(i,style);
                index ++;
            }
        }
        //创建大小王
        Card xiaowang = new Card(15,CardStyle.HONG_TAO);
        Card dawang = new Card(16,CardStyle.HONG_TAO);
        initCards[initCards.length -1] = dawang;
        initCards[initCards.length -2] = xiaowang;
        play();
    }
    public void play(){
        for (int i = 0; i < 17; i ++){
            for (Player player : this.players)
            {
                Random r = new Random();
                int index = r.nextInt(this.initCards.length);
                player.getCards()[i] = this.initCards[index];
                Card [] newCards = new Card[this.initCards.length -1];
                this.initCards[index] = this.initCards[this.initCards.length -1];
                newCards = Arrays.copyOf(this.initCards,this.initCards.length -1);
                this.initCards = newCards;
            }
        }
        Random r = new Random();
        int index = r.nextInt(players.length);
        int playCardsLen = players[index].getCards().length;
        int initCardLen = this.initCards.length;
        players[index].getCards()[playCardsLen - 1] = this.initCards[initCardLen - 1];
        players[index].getCards()[playCardsLen - 2] = this.initCards[initCardLen - 2];
        players[index].getCards()[playCardsLen - 3] = this.initCards[initCardLen - 3];
        players[index].setDizhu(true);
        for (Player player : players)
        {
            for (int i = 0; i < player.getCards().length; i ++){
                for (int j = i + 1; j < player.getCards().length; j ++){
                    if(player.getCards()[i] != null && player.getCards()[j] != null){
                        if(player.getCards()[i].getRealvalue() > player.getCards()[j].getRealvalue()){
                            Card temp = player.getCards()[j];
                            player.getCards()[j] = player.getCards()[i];
                            player.getCards()[i] = temp;
                        }
                    }

                }
            }
        }
    }
    public Card[] getInitCards() {
        return initCards;
    }
    public void setInitCards(Card[] initCards) {
        this.initCards = initCards;
    }
    public Player[] getPlayers() {
        return players;
    }
    public void setPlayers(Player[] players) {
        this.players = players;
    }

}

